import Cookies from 'js-cookie';


export function isLogin() {
  const {token, uid} = getLogin();

  return token && uid;
}

export function getLogin() {
  // let userInfo = localStorage.getItem(consts.CACHE_USER_INFO);
  // if (userInfo) {
  //   userInfo = JSON.parse(userInfo);
  // }

  return {
    token: Cookies.get(consts.CACHE_TOKEN, {domain: _.getFirstLevelDomain({change: false})}),
    uid: Cookies.get(consts.CACHE_UID, {domain: _.getFirstLevelDomain({change: false})}),
    username: Cookies.get(consts.CACHE_USERNAME, {domain: _.getFirstLevelDomain({change: false})}),
  };
}

export function setLogin(data) {
  Cookies.set(consts.CACHE_TOKEN, data.token, {expires: moment().add(30, 'm').toDate(), domain: _.getFirstLevelDomain({change: false})});
  Cookies.set(consts.CACHE_UID, data.id, {expires: moment().add(30, 'm').toDate(), domain: _.getFirstLevelDomain({change: false})});
  Cookies.set(consts.CACHE_USERNAME, data.username, {expires: moment().add(30, 'm').toDate(), domain: _.getFirstLevelDomain({change: false})});
}

export function logout() {
  Cookies.remove(consts.CACHE_TOKEN, {domain: _.getFirstLevelDomain({change: false})});
  Cookies.remove(consts.CACHE_UID, {domain: _.getFirstLevelDomain({change: false})});
  Cookies.remove(consts.CACHE_USERNAME, {domain: _.getFirstLevelDomain({change: false})});
}
